/*
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-27     chenbin
 */
#include "fclear.h"
#if (FBOOT_USING_FCLEAR == 1)

#define DBG_ENABLE
#define DBG_SECTION_NAME "fclear"
#define DBG_LEVEL DBG_INFO
#include <rtdbg.h>

#include "fal.h"

int fclear_fal(const char *part_name)
{
	const struct fal_partition *part = NULL;
	
	part = fal_partition_find(part_name);
	if (part != NULL)
	{
		LOG_I("erase(%s) all.", part_name);
		fal_partition_erase_all(part);
	}
	return 0;
}

#if (FBOOT_USING_FILESYSTEM == 1)

#ifdef RT_USING_DFS
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#endif

#include <unistd.h>
//#include <dirent.h>
//#include <fcntl.h>
//#include <sys/stat.h>
//#include <sys/statfs.h>
//#include <sys/time.h>
//#include <sys/ioctl.h>

int fclear_fs(const char *file_name)
{
	return unlink(file_name);
}

#endif




#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
#include <finsh.h>

static const char *help_info[] =
{
	"fclear fal <fal part name>\n\r",
#if (FBOOT_USING_FILESYSTEM == 1)
	"fclear fs  <file path> \n\r",
#endif
};

void fclear_cmd(rt_uint8_t argc, char **argv)
{
	if (argc > 2)
	{
		const char *cmd = argv[1];
		if (!rt_strcmp(cmd, "fal"))
		{
			fclear_fal(argv[2]);
			return;
		}
#if (FBOOT_USING_FILESYSTEM == 1)
		if (!rt_strcmp(cmd, "fs"))
		{
			fclear_fs(argv[2]);
			return;
		}
#endif
	}
	{
		rt_kprintf("usage:\n");
		for (int i = 0; i < sizeof(help_info) / sizeof(char *); i++)
		{
			rt_kputs(help_info[i]);
		}
		rt_kprintf("\n");
	}
}
MSH_CMD_EXPORT_ALIAS(fclear_cmd, fclear, fclear [fal/fs]);


#endif

#endif


